package com.tedu.myboot.controller;

import com.tedu.myboot.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    private static File userDir;

    static {
        userDir = new File("users");
        if (!userDir.exists()) {
            userDir.mkdirs();
        }
    }

    @RequestMapping("/myweb/reg")
    public void reg(User user, HttpServletResponse response) {
        System.out.println(user);
        File userFile = new File(userDir, user.getUsername() + ".obj");
        try (
                ObjectOutputStream oos = new ObjectOutputStream(
                        new FileOutputStream(userFile)
                )
        ) {
            oos.writeObject(user);
            response.sendRedirect("/myweb/reg_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @RequestMapping("/myweb/login")
    public void login(User user,HttpServletResponse response) throws IOException {
        //1通过request获取表单信息

        if(user.getUsername().isEmpty()||user.getPassword().isEmpty()){//如果没有输入内容则提示输入信息有误
            response.sendRedirect("/myweb/login_info_error.html");
            return;
        }

        //2处理登录
        File userFile = new File(userDir,user.getUsername()+".obj");
        if(userFile.exists()){//该文件存在，才说明用户名输入对了
            //将该用户注册信息读取回来，用于比对密码
            try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(userFile));){
                 User user1 = (User)ois.readObject();
                if(user.getPassword().equals(user.getPassword())){//密码正确，登录成功
                    response.sendRedirect("/myweb/login_success.html");
                    return;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //只要执行到这里，就说明登录失败了1:文件不存在(用户名不对) 2:密码不对
        response.sendRedirect("/myweb/login_fail.html");
    }
    @RequestMapping("/myweb/showAllUser")
    public void showAllUser(HttpServletRequest request, HttpServletResponse response) {
        List<User> userList = new ArrayList<>();
        File[] users = userDir.listFiles(f->f.getName().endsWith(".obj"));
        for(File userFile : users){
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(
                                    userFile
                            )
                    )
            ){
                User user = (User)ois.readObject();
                userList.add(user);
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /*
            2生成一个HTML页面(动态页面)并包含所有用户数据
         */
        //添加响应头，用于将来告知浏览器该动态数据是一个html页面
        response.setContentType("text/html;charset=utf-8");

        PrintWriter pw = null;
        try {
            pw = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>用户列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<h1>用户列表</h1>");
        pw.println("<table border=\"1\">");
        pw.println("<tr>");
        pw.println("<td>用户名</td>");
        pw.println("<td>密码</td>");
        pw.println("<td>昵称</td>");
        pw.println("<td>年龄</td>");
        pw.println("</tr>");
        for(User user : userList) {
            pw.println("<tr>");
            pw.println("<td>"+ user.getUsername()+"</td>");
            pw.println("<td>"+ user.getPassword()+"</td>");
            pw.println("<td>"+ user.getNickname()+"</td>");
            pw.println("<td>"+ user.getAge()+"</td>");
            pw.println("</tr>");
        }
        pw.println("</table>");
        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");




    }
}
